package com.open.capacity.task.controller;

import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.SysTask;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.log.annotation.LogAnnotation;
import com.open.capacity.task.model.TaskType;
import com.open.capacity.task.model.TreeNode;
import com.open.capacity.task.service.TaskService;

import com.open.capacity.task.utils.BuildTree;
import com.open.capacity.task.utils.GetDate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Api(tags = "Task API")
@RequestMapping("/task")
public class TaskController {
    @Autowired
    TaskService taskService;

    @PostMapping("/findTasks")
    @ApiOperation(value = "查询任务（可条件查询）")
    public PageResult<SysTask> findTasks(@RequestParam Map<String, Object> conditions) {
        int count = taskService.findCounts(conditions);
        List<SysTask> tasks = taskService.findTasks(conditions);
        return PageResult.<SysTask>builder().data(tasks).count((long) count).code(0).build();
    }


    @PostMapping("/findTaskAreas")
    @ApiOperation(value = "查询任务范围（可条件查询）")
    public Result findTaskAreas(@RequestParam Map<String, Object> conditions) {
        TaskType taskType = taskService.findTaskAreas(conditions);
        return null;
    }


    /**
     * 发布任务接口
     *
     * @param
     * @return
     */
    @PostMapping("/publishTaskOrUpdateTask")
    @ApiOperation(value = "发布任务")
    public Result publishTaskOrUpdateTask(@RequestBody SysTask task) {
        return taskService.publishTaskOrUpdateTask(task);
    }

//    /**
//     * 更新任务接口(包含标题，内容，发布状态，积分，撤销)
//     *
//     * @param sysTask
//     * @return
//     */
//    @PostMapping("/updateTask")
//    @ApiOperation(value = "更新任务(包含标题，内容，发布状态，积分，撤销)")
//    public Result updateTask(@RequestParam Map<String, String> sysTask) throws ControllerException {
//        //获取更新时间
//        String updtim = GetDate.get();
//        sysTask.put("updtim", updtim);
//        if (sysTask.get("integr") != null) {
//            int point = Integer.parseInt(sysTask.get("integr"));
//            point += 5;
//            sysTask.put("integr", String.valueOf(point));
//        }
//        Integer count = taskService.updateTask(sysTask);
//        return count == 1 ? Result.succeed("更新成功") : Result.failed("更新失败");
//    }

    /**
     * 新增任务范围（前端，后台......）
     *
     * @param taskType
     * @return
     */
    @PostMapping("/addTaskType")
    @ApiOperation(value = "新增任务范围（前端，后台......）")
    public Result addTaskType(@RequestParam Map<String, String> taskType) {
        taskType.put("status", "1");
        taskType.put("cretim", GetDate.get());
        Integer count = taskService.addTaskType(taskType);
        return count == 1 ? Result.succeed("添加成功") : Result.failed("添加失败");
    }

    /**
     * 更新任务范围（前端，后台......）
     *
     * @param taskType
     * @return
     */
    @PostMapping("/updateTaskType")
    @ApiOperation(value = "更新任务范围（前端，后台......）")
    public Result updateTaskType(@RequestParam Map<String, String> taskType) {
        Integer count = taskService.updateTaskType(taskType);
        return count == 1 ? Result.succeed("修改成功") : Result.failed("修改失败");
    }

    /**
     * 新增任务类型（普通，紧急......）
     *
     * @param type
     * @return
     */
    @PostMapping("/addTaskLevel")
    @ApiOperation(value = "新增任务类型：普通，紧急。。。")
    public Result addTaskLevel(@RequestParam Map<String, Object> type) {
        type.put("status", 1);
        type.put("cretim", GetDate.get());
        Integer count = taskService.addTaskLevel(type);
        return count == 1 ? Result.succeed("添加成功") : Result.failed("添加失败");
    }

    /**
     * 修改任务类型：普通，紧急。。。
     *
     * @param type
     * @return
     */
    @PostMapping("/updateTaskLevel")
    @ApiOperation(value = "修改任务类型：普通，紧急。。。")
    public Result updateTaskLevel(@RequestParam Map<String, Object> type) {
        Integer count = taskService.updateTaskLevel(type);
        return count == 1 ? Result.succeed("修改成功") : Result.failed("修改失败");
    }

    /**
     * 任务范围查询
     *
     * @param params
     * @return data{id:"",parentId:"",name:"",value:"",children:[]}
     */
    @GetMapping("/findTaskAreaTreeNode")
    public PageResult findTaskAreaTreeNode(@RequestParam Map<String, Object> params) {
        List<TreeNode> treeNodes = new BuildTree().buildTree(taskService.findTaskAreaTreeNode(params));
        System.out.println(treeNodes);
        return PageResult.<TreeNode>builder().data(treeNodes).code(0).count((long) treeNodes.size()).build();
    }

    /**
     * 根据taskid修改任务状态
     *
     * @param taskid
     * @return
     */
    @PostMapping("/delOrRecByTaskId")
    public Result delOrRecByTaskId(@RequestParam int taskid) {
        return taskService.delOrRecByTaskId(taskid);
    }

    /**
     * 查询用户发布的任务
     *
     * @param params
     * @return
     */
    @GetMapping("/findMyPublishTasks")
    public PageResult findMyPublishTasks(@RequestParam Map<String, Object> params) {
        return taskService.findMyPublishTasks(params);
    }

    /**
     * 当前任务的回复列表
     *
     * @param params
     * @return
     */
    @GetMapping("/findMyPublishTaskAdopt")
    public PageResult findMyPublishTaskAdopt(@RequestParam Map<String, Object> params) {
        return taskService.findMyPublishTaskAdopt(params);
    }

    /**
     * 采纳回答
     *
     * @param params
     * @return
     */
    @PostMapping("/adoptReply")
    public Result adoptReply(@RequestBody Map<String, Object> params) {
        return taskService.adoptReply(params);
    }

    /**
     * 截止任务
     *
     * @param params
     * @return
     */
    @PostMapping("/endTaskByTaskid")
    public Result endTaskByTaskid(@RequestBody Map<String, Integer> params) {
        return taskService.endTaskByTaskid(params);
    }

    /**
     * 当前任务的评论
     *
     * @param params
     * @return
     */
    @GetMapping("/findTaskComment")
    public PageResult findTaskComment(@RequestParam Map<String, Object> params) {
        return taskService.findTaskComment(params);
    }

    @PostMapping("/getTaskByTaskid")
    public Result getTaskByTaskid(@RequestBody Map<String,Object> params) {
        return taskService.getTaskByTaskid(params);
    }

    /**
     * 查询已接取任务
     * @param params
     * @return
     */
    @GetMapping("/findMyInterceptTasks")
    public PageResult findMyInterceptTasks(@RequestParam Map<String, Object> params) {
        return taskService.findMyInterceptTasks(params);
    }

    /**
     * 线上留言提交
     * @param params
     * @return
     */
    @PostMapping("/onlineReply")
    public Result onlineReply(@RequestBody Map<String,Object> params) {
        return taskService.onlineReply(params);
    }

    /**
     * 修改留言界面赋值
     * @param sysTask
     * @return
     */
    @PostMapping("/findByTaskReplyId")
    public SysTask findTaskReply(@RequestBody SysTask sysTask) {
        return taskService.findTaskReply(sysTask);
    }

    /**
     * 修改在线留言
     * @param params
     * @return
     */
    @PostMapping("/updateOnlineReply")
    public Result updateOnlineReply(@RequestBody Map<String,Object> params) {
        return taskService.updateOnlineReply(params);
    }
}
